#! /bin/sh
# sed -i 's/\r$//' configure.sh
# description:一键部署Nginx+uWSGI+Django
# Author: Juner

# 这里不需要动,保证脚本目录所在的目录中包含了项目目录即可
MANAGE_DIR=`pwd`
#**************************您的配置区域其他的无需修改**************************
# [config1]项目名称
PROJECT_NAME="teacher"
# [config2]nginx域名或者IP地址和端口
SERVER_NAME="148.70.61.219"
SERVER_PORT=80

#******************************************************************************
# 项目目录
PROJECT_DIR=$MANAGE_DIR/$PROJECT_NAME
#Python安装目录
PYTHON_DIR=/usr/local/gion/python3/
# 初始化python环境配置，如果没有安装python进行安装
init_python_env(){

# 检查Python是否安装
if [ -d  $PYTHON_DIR ]; then
echo -e "\n\033[33m Python已安装........\033[0m"
return 0
elif [ ! -d $PYTHON_DIR ]; then
echo -e "\033[32m Python未安装........\033[0m"
fi

echo -e "\033[32m============Python3 开始安装============\033[0m"
#yum安装工具必须使用python2.7
echo "modify yum Default config file"
sed -i 's/python/python2.7/' /usr/bin/yum
sed -i 's/python/python2.7/' /usr/libexec/urlgrabber-ext-down

wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz
mkdir /usr/local/gion/
mkdir /usr/local/gion/source
tar -xvzf Python-3.6.7.tgz -C /usr/local/gion/source/
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel gcc make
cd /usr/local/gion/source/Python-3.6.7
mkdir /usr/local/gion/python3
./configure --prefix=/usr/local/gion/python3
make
make install
cat > /etc/profile.d/python3.sh<<EOF
export PATH="$PATH:/usr/local/gion/python3/bin"
EOF
source /etc/profile
rm -rfv /usr/bin/python
ln -s /usr/local/gion/python3/bin/python3 /usr/bin/python
echo $PATH

#install python package
pip3 install --upgrade pip
pip3 install appdirs==1.4.3
pip3 install Django==2.1.7
pip3 install uWSGI
pip3 install packaging==16.8
pip3 install pyparsing==2.2.0
pip3 install pytz==2018.9
pip3 install six==1.10.0

}

# 检查项目目录是否存在
check_project(){
if [ ! -d $PROJECT_DIR ]; then
echo -e "\n\033[33m PROJECT_DIR 没有指定,请指定项目目录\033[0m"
exit 0
elif [ -d $PROJECT_DIR ]; then
echo -e "\033[32m当前配置项目PROJECT_DIR:$PROJECT_DIR........\033[0m"
fi


}


# 创建脚本管理配置
make_script_init(){
echo -e "\033[32m开始创建script目录........\033[0m"
mkdir $MANAGE_DIR/script
echo -e "\033[32m开始创建管理文件........\033[0m"
cat > $MANAGE_DIR/script/manage.sh<<EOF
#! /bin/sh
# chkconfig: 345 85 15
# 上面一行注释:哪些Linux级别需要启动manage_teacher(3,4,5)；启动序号(85)；关闭序号(15)。
# description: manage script is the $PROJECT_NAME daemon.
# Author: juner
# 重载环境变量
export PATH=\$PATH:/usr/local/gion/python3/bin
# 指定项目目录
PROJECT_DIR=$PROJECT_DIR
# 指定脚本目录在哪里
SCRIPTS_DIR=$MANAGE_DIR/script
# 描述
DESC="$PROJECT_NAME daemon"
# 名称
NAME=$PROJECT_NAME
# 脚本名称
SCRIPT_FILENAME="manage.sh"
SCRIPTNAME=$MANAGE_DIR/script
# PID
PID="uwsgi.pid"

# 启动函数
d_start(){
# 进入到项目目录
cd $MANAGE_DIR/script/
# 判断这个PID是否存在
if [ ! -f "uwsgi.pid" ];then
echo -e "\n\033[34m$NAME 项目启动中........\033[0m"
# 如果不存在执行
uwsgi --ini uwsgi.ini
killall nginx
systemctl start nginx.service
# 自动收集静态文件
cd $MANAGE_DIR/$PROJECT_NAME&& python manage.py collectstatic --noinput
echo -e "\n\033[32m$NAME 项目启动完成........\033[0m"
exit 0
fi
echo -e "\n\033[33m$NAME 项目已启动请勿重复启动\033[0m"
}
# 关闭函数
# 关闭项目
d_stop(){
# 进入脚本目录
cd $MANAGE_DIR/script/
# 判断这个pid文件是否存在
if [ ! -f "uwsgi.pid" ];then
# 这个项目已经关闭了
echo -e "\n\033[33m$NAME 项目已经关闭了请先启动\033[0m"
fi
echo -e "\n\033[34m$NAME 项目关闭中........\033[0m"
echo -e "\nStop $DESC: $NAME"
# 如果没有关闭
uwsgi --stop uwsgi.pid
# 是否停掉Nginx根据实际需要来操作~~！因为Nginx有对静态文件缓存［注意］
killall nginx
systemctl stop nginx.service
echo -e "\n\033[32m$NAME 项目关闭完成........\033[0m"
}

d_restart(){
d_stop
sleep 1
d_start
}

case "\$1" in
start)
echo -e "\nstarting $DESC: $NAME"
d_start
;;
stop)
echo -e "\nStop $DESC: $NAME"
d_stop
;;
restart)
echo -e "\nRestart $DESC: $NAME"
d_restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 3
;;
esac
EOF
chmod 777 $MANAGE_DIR/script/manage.sh
echo -e "\033[32m开始创建.service........\033[0m"
cat > $MANAGE_DIR/script/$PROJECT_NAME.service<<EOF
[Unit]
Description=$PROJECT_NAME
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
User=users
Group=users
#PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=$MANAGE_DIR/script/manage.sh  start
ExecReload=$MANAGE_DIR/script/manage.sh restart
ExecStop=$MANAGE_DIR/script/manage.sh   stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
chmod 755 $MANAGE_DIR/script/$PROJECT_NAME.service

echo -e "\033[32m开始添加uwsgi.ini配置文件........\033[0m"
cat > $MANAGE_DIR/script//uwsgi.ini<<EOF
[uwsgi]
# 项目目录
chdir=$PROJECT_DIR
# 启动uwsgi的用户名和用户组
uid=root
gid=root
# 指定项目的application
module=$PROJECT_NAME.wsgi:application
# 指定sock的文件路径
socket=$MANAGE_DIR/script/uwsgi.sock
# 启用主进程
master=true
# 进程个数
workers=5
pidfile=$MANAGE_DIR/script/uwsgi.pid
# 自动移除unix Socket和pid文件当服务停止的时候
vacuum=true
# 序列化接受的内容，如果可能的话
thunder-lock=true
# 启用线程
enable-threads=true
# 设置自中断时间
harakiri=30
# 设置缓冲
post-buffering=4096
# 设置日志目录
daemonize=$MANAGE_DIR/script/uwsgi.log
EOF
echo  "STATIC_ROOT = os.path.join(BASE_DIR, 'static_all')">> $MANAGE_DIR/$PROJECT_NAME/settings.py
}

make_nginx_init(){
echo -e "\033[32m初始化Nginx........\033[0m"
cat > /etc/yum.repos.d/nginx.repo<<EOF
[nginx]
name=nginx repo
# 下面这行centos根据你自己的操作系统修改比如：OS/rehel
# 7是你Linux系统的版本，可以通过URL查看路径是否正确
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1
EOF
yum -y install nginx
echo -e "\033[32m添加Nginx配置文件........\033[0m"

cat > /etc/nginx/conf.d/$PROJECT_NAME.conf<<EOF
    server {
                    listen $SERVER_PORT;
                    server_name $SERVER_NAME ;
                    access_log  /var/log/nginx/access.log  main;
                    charset  utf-8;
                    gzip on;
                    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;

                    error_page  404           /404.html;
                    error_page   500 502 503 504  /50x.html;
                    # 指定项目路径uwsgi
                    location / {
                        include uwsgi_params;
                        uwsgi_connect_timeout 30;
                        uwsgi_pass unix:$MANAGE_DIR/script/uwsgi.sock;
                    }
                    # 指定静态文件路径
                    location /static/ {
                        alias  $MANAGE_DIR/$PROJECT_NAME/static_all/;
                        index  index.html index.htm;
                    }

    }
EOF
}
add_server(){
echo -e "\033[32m添加管理至服务........\033[0m"
cp $MANAGE_DIR/script/$PROJECT_NAME.service /usr/lib/systemd/system
# 启动服务
echo -e "\033[32m设置开机自启动，启动服务........\033[0m"
systemctl start $PROJECT_NAME.service
systemctl enable $PROJECT_NAME.service
}

init_python_env
check_project
make_script_init
make_nginx_init
add_server